﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleExpression
{
    public class EntityMetadata
    {
        private string platformName;
        private string physicalName;
        private string schema;
        private string alias;

        private PropertyMetadataCollection properties;
        private IDictionary<string, int> propertiesByName;

        public EntityMetadata()
        { 
        
        }

        public EntityMetadata(string platformName)
        {
            this.platformName = platformName;
            this.physicalName = platformName;
            this.schema = "Core";
            this.properties = new PropertyMetadataCollection();
            this.propertiesByName = new Dictionary<string, int>();
        }

        public PropertyMetadataCollection Properties
        {
            get { return this.properties; }
            set { this.properties = value; }
        }

        public string Schema
        {
            get
            {
                return this.schema;
            }
        }

        public string PhysicalName
        {
            get
            {
                return this.physicalName;
            }
        }

        public string PlatformName
        {
            get
            {
                return this.physicalName;
            }
        }

        public string Alias
        {
            get { return this.alias; }
            set { this.alias = value; }
        }

        public PropertyMetadata this[string propertyName]
        {
            get { return this.properties[this.propertiesByName[propertyName]]; }
        }

        public static EntityMetadata GetEntity(string platformName)
        {
            EntityMetadata entityMetadata = new EntityMetadata(platformName);

            if (platformName == "Customer")
                SetCustomerProperties(entityMetadata);
            else
            {
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo1", PlatformName = "Campo1" });
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo2", PlatformName = "Campo2" });
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo3", PlatformName = "Campo3" });
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo4", PlatformName = "Campo4" });
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo5", PlatformName = "Campo5" });
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo6", PlatformName = "Campo6" });
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo7", PlatformName = "Campo7" });
                entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Campo8", PlatformName = "Campo8" });
            }

            return entityMetadata;
        }

        private static void SetCustomerProperties(EntityMetadata entityMetadata)
        {
            entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "CustomerId", PlatformName = "CustomerId" });
            entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "Age", PlatformName = "Age" });
            entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "FirstName", PlatformName = "FirstName"  });
            entityMetadata.AddProperty(new PropertyMetadata(entityMetadata) { PhysicalName = "LastName", PlatformName="LastName" });
        }

        public void AddProperty(PropertyMetadata propertyMetadata)
        {
            this.properties.Insert(this.properties.Count, propertyMetadata);
            this.propertiesByName.Add(propertyMetadata.PhysicalName, this.properties.Count - 1);
        }
    }
}
